/**
 * Created by zhangxiaoyu on 2016/4/1.
 */
angular.module('hyt.scanCodeCtrl',['ngCordova'])
    .controller('ScanCodeListCtrl',function($scope,ZdListWqFactory,ZdListYqFactory,ZdListSearchFactory,$ionicPopup,ENV,$ionicLoading,Storage,$stateParams){
        $scope.showloading = true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        var bm_id=Storage.get('bm_id');
        var dw_id=Storage.get('dw_id');
        ZdListWqFactory.getZdWqLists(sid,user_id,yh_id,zd_id,$stateParams.cph,$stateParams.sfz,$stateParams.kssj,$stateParams.jssj,dw_id);
        $scope.$on('zdListWqs.zdUpdated', function() {
            var zdWqFactory=ZdListWqFactory.getWqZds();
            $scope.zdWqData = zdWqFactory.data;
            $scope.getClzt=function(clzt){
                switch (clzt){
                    case 1 :
                        $scope.clzt = '装车';
                        break;
                    case 2 :
                        $scope.clzt = '发车';
                        break;
                    case 3 :
                        $scope.clzt = '到货';
                        break;
                    default :
                        break;
                }
                return $scope.clzt;
            }
            $scope.showloading = false;

        });
        $scope.doRefreshWq = function(){
            ZdListWqFactory.getZdWqLists(sid,user_id,yh_id,zd_id,$stateParams.cph,$stateParams.sfz,$stateParams.kssj,$stateParams.jssj,dw_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.loadMoreWq = function(){
            ZdListWqFactory.getMoreWqCyds(sid,user_id,yh_id,zd_id,$stateParams.cph,$stateParams.sfz,$stateParams.kssj,$stateParams.jssj,dw_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        $scope.hasNextPageWq =function(){
            return ZdListWqFactory.hasNextPageWqs();
        }

    })
    .controller('ScanCodeCtrl',function($scope,$cordovaScanner,$ionicPopup,Storage,ScanCodeFactory,$ionicLoading,$timeout){
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var shbm_id=Storage.get('bm_id');
        var api =Storage.get('fwqdz');
        var zsl,yqsl;
        $scope.scanBarcode = function(cph,zcdh,zcd_id) {
            ScanCodeFactory.cydsl(sid,yh_id,user_id,shbm_id,zcd_id);
            $scope.$on('Cydsl.update', function() {
                var result=ScanCodeFactory.getResult();
                zsl=result.zongshu;
                yqsl=result.yiqian;
            });
            $timeout(function(){
                var option = {"cph":cph,"zcdh":zcdh,"yh_id":yh_id,"dw_id":dw_id,"sid":sid,"user_id":user_id,"shbm_id":shbm_id,"api":api,"zsl":zsl,"yqsl":yqsl,"zcd_id":zcd_id};
                $cordovaScanner.startScan(function(success){

                },function(error){

                },option);
            },200);
        }
    })
    .controller('ScanCodeSearchCtrl',function($scope,$ionicLoading,$ionicPopup,$state,CarFactory,SfzFactory,Storage,$stateParams,$filter){
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var zd_id=Storage.get('zd_id');
        //CarFactory.getCphList(sid,user_id,yh_id,dw_id,zd_id);
        //获得车牌号列表
        $scope.daozhan={
            cph:'',
            cl_id:''
        };
        $scope.daozhan.cph=$stateParams['cph'];
        //$scope.$on('List.cphList',function(){
        //    var list=CarFactory.getCph();
        //    $scope.cphList=list.ACTION_INFO.RESULT_LIST;
        //    console.log($scope.cphList);
        //});
        SfzFactory.getSfzList(sid,user_id,yh_id,dw_id);
        //获得站点列表
        $scope.$on('List.sfzList',function(){
            var list=SfzFactory.getSfz();
            $scope.sfzList=list.ACTION_INFO.RESULT_LIST;
            console.log($scope.sfzList);
        });
        $scope.chooseCph = function () {
            $state.go("scanCarList");
        };
        $scope.chooseSfz = function () {
            var alertPopup = $ionicPopup.alert({
                title: '请选择站点',
                scope: $scope,
                template: '<ion-list  ng-repeat="item in sfzList"><ion-radio ng-bind="item.bmmc" style="text-align: center;margin-top: 3px;" ng-click="selectSfz(item.bmmc,item.bm_id)"></ion-radio></ion-list>',
                okText: '取消'
            });
            alertPopup.then(function (res) {
                if (res) {

                } else {

                }
            });
            $scope.selectSfz = function (bmmc, bm_id) {
                $scope.bm_id = bm_id;
                $scope.bmmc = bmmc;
                alertPopup.close();
            };
        };
        var t = new Date();
        var iToDay=t.getDate();
        var iToMon=t.getMonth();
        var iToYear=t.getFullYear();
        var iTDate=new Date(iToYear,iToMon,(iToDay-30));
        $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
                date: iTDate,
                mode: 'date'
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        iTDate=d;
                        $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
                    });
                }
            });
        };

        var endTime = new Date();
        $scope.endDate=$filter('date')(endTime,'yyyy-MM-dd');
        $scope.showEndDate = function(){
            console.log('EndDate');
            var optionsEnd = {
                date : endTime,
                mode : 'date'
            };
            datePicker.show(optionsEnd,function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        endTime=d;
                        $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
                    });
                }
            });

        };
        $scope.searchZcd = function(cph,bmmc){
            if(cph==''){
                $ionicLoading.show({
                    template : '请输入车牌号',
                    duration : 600
                });
            }else {
                $state.go('scanCode',{cph:cph,sfz:bmmc,kssj:$scope.startDate,jssj:$scope.endDate});
            }
        };
    })
